
package JET.SA;

import JET.Centers.ControlCenter;
import JET.Centers.Graphics.GUICenter.UtilTabs;
import JET.Frames.DataModels.DataModelsFactory;
import JET.Frames.DataModels.SceneTreeModel;
import JET.Scene;
import JET.UtilPanels.UPSceneInspector;
import JET.physics.bodies.RigidBody;
import JET.physics.interfaces.SceneTreeNode;
import javax.swing.tree.TreePath;

/**
 *
 * @author raos
 */
public class SA_RigidBody {
    private static ControlCenter CC = ControlCenter.getInstance();

    public static RigidBody addRigidBodyToScene( Scene scene, RigidBody rb) {
        scene.getJETContext().addRigidBody(rb);

        //if (updateSceneTreeSelection) DataModelsFactory.getSceneTreeModel().fireSpringBodyListChanged();
        //SA_SpringBody.setActualRigidBody(sb, scene, updateSceneTreeSelection);

        return rb;
    }

    public static void removeRigidBody(RigidBody rb, Scene scene) {
        if (rb != null) {

            // Remove spring body from engine
            scene.getJETContext().removeRigidBody(rb);

            // If thera are no more bodies in the engine do apropriate stuff with editor
            if (scene.getJETContext().getRigidBodyList().isEmpty()) {
                SA_RigidBody.setCurrentRigidBody(null, scene, true);

            } // In other case, set next sb to be actual spring body that editor works on
            else {
                RigidBody arb = scene.getJETContext().getRigidBodyList().get(scene.getJETContext().getRigidBodyList().size() - 1);
                SA_RigidBody.setCurrentRigidBody(arb, scene, true);
            }

        }
    }

    public static RigidBody setCurrentRigidBody(RigidBody rb, Scene scene, boolean updateSceneTreeSelection) {
        if (scene.EC.currentBody!=null && updateSceneTreeSelection) {
            UPSceneInspector upSceneInspector = (UPSceneInspector) CC.GUIC.getUtilTab(UtilTabs.SceneInspector);
            //SceneTreeNode node = scene.getEngine().sceneTreeNode;
            SceneTreeNode node = DataModelsFactory.getSceneTreeModel().sceneRootNode;
            TreePath path = new TreePath(new Object[]{  node,
                                                        node.getChild(SceneTreeModel.SceneRootNode.NODE_INDEX_SPRING_BODIES),
                                                        scene.EC.currentBody
                                                 });
            upSceneInspector.getSceneTree().removeSelectionPath(path);
        }

        scene.EC.currentBody = rb;

        if (rb != null) {
//            SA_Dialogs.fillPropertiesDialogWithSpringBodyData(sb);
            rb.updateBoundingArea();
//            if (CC.GUIC.materialsDialog!=null)
//                CC.GUIC.materialsDialog.setSelectedCell(sb.getMaterial(), sb.getMaterial());

            if (updateSceneTreeSelection) {
                UPSceneInspector upSceneInspector = (UPSceneInspector) CC.GUIC.getUtilTab(UtilTabs.SceneInspector);
                //SceneTreeNode node = scene.getEngine().sceneTreeNode;
//                SceneTreeNode node = DataModelsFactory.getSceneTreeModel().sceneRootNode;
//                TreePath path = new TreePath(new Object[]{  node,
//                                                            node.getChild(SceneTreeModel.SceneRootNode.NODE_INDEX_SPRING_BODIES),
//                                                            rb
//                                                     });
            }

            CC.GUIC.repaintAllSceneTabs();
        }

        return rb;
    }
}
